package list;

public class HeBingYouXuLianBiao {

    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(3);
        head.next.next= new Node(9);
        head.next.next.next= new Node(11);
        head.next.next.next.next= new Node(14);

        Node head2 = new Node(4);
        head2.next = new Node(5);
        head2.next.next= new Node(11);
        head2.next.next.next= new Node(15);
        head2.next.next.next.next= new Node(17);

        Node newNode = heBing(head,head2);

        while (newNode != null){//只要引用了节点就继续访问，null是最后一个
            System.out.println(newNode.value);//输出
            newNode = newNode.next;//获取下一个节点
        }


    }

    private static Node heBing(Node head ,Node head2) {
        Node preHead = new Node(0);
        Node n1 = head, n2=head2;
        Node n3 = preHead;
        while (n1 != null && n2 != null){
            if(n1.value< n2.value){
                n3.next = new Node(n1.value);
                n1=n1.next;
            }else {
                n3.next = new Node(n2.value);
                n2 = n2.next;
            }
            n3 = n3.next;
        }
        if(n1 != null){
            while (n1 != null){
                n3.next = new Node(n1.value);
                n1= n1.next;
                n3 = n3.next;
            }
        }else {
            while (n2 != null){
                n3.next = new Node(n2.value);
                n2= n2.next;
                n3 = n3.next;
            }
        }
        return preHead.next;
    }


    static class Node{
        private int value;
        private Node prev;
        private Node next;
        public Node(int value){
            this.value=value;
        }
    }
}
